<?php

class ShangpinController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
                //设置首页引用css
                $this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/product.css');
                //设置title
                $this->view->headTitle("珠宝玉石饰品");
    }

    public function indexAction()
    {
		//设置首页引用js
		$this->view->headScript()->appendFile($this->view->baseUrl().'/js/index_flash.js');
		//设置关键词（seo）
		$this->view->headMeta()->appendName('keywords', '翡翠,岫玉,和田玉,手镯,戒子,手链,吊坠,项链,挂件,玉牌,玉石商品,玉石礼品');
		//设置描述（seo）
		$this->view->headMeta()->appendName('description','展示翡翠饰品、岫玉工艺品、和田玉饰品、各类玉石商品及玉石礼品。包含手镯、戒指、吊坠、挂件、项链、玉牌等多种商品');
        $db_taobaoitems=new Application_Model_DbTable_TaobaoItems();
        $hotItems=$db_taobaoitems->getHotItems(2,16);
        $this->view->hotItems=$hotItems;
        $this->view->newItemsList=$db_taobaoitems->getNewItems(2,16);
        $this->view->promotionItemsList=$db_taobaoitems->getPromotionItems(2,16);
        
    }

    public function contentAction()
    {
        // action body
        $num_iid=$this->getRequest()->getParam('id');
        $db_taobaoitems=new Application_Model_DbTable_TaobaoItems();
        $articleModel=new Application_Model_DbTable_Article();
        $item=$db_taobaoitems->getItemsByNumiid($num_iid);
        $count=$item['count_num'];
        $count+=1;
        $db_taobaoitems->updateCount($num_iid, $count);
        $db_priceMark=new Application_Model_DbTable_Pricemark();
        $itemPriceMark=$db_priceMark->getPriceByNumiid($num_iid);
        if(null!=$itemPriceMark){
            if($itemPriceMark['min_price']!=$itemPriceMark['max_price']){
                $item['priceMark']=$itemPriceMark;
            }
        }
        //设置title
        $this->view->headTitle()->prepend($item['title']);
        //设置关键词（seo）
        $this->view->headMeta()->appendName('keywords', $item['item_keywords']);
        //设置描述（seo）
        $this->view->headMeta()->appendName('description','淘岫玉网站推广商品,'.$item['title'].'。淘宝网有售');
        $keywords=explode(",", $item['item_keywords']);
        $this->view->keywds=$keywords;
        $this->view->item=$item;
        $this->view->sideBar=1;
        //边栏热门文章列表
        $this->view->hotArticles=$articleModel->getHotArticle(13);
        $itemsModel=new Application_Model_DbTable_TaobaoItems();
        $this->view->hotItems=$itemsModel->getSidebarHotItems(8,$num_iid);
        
    }
    
    public function listAction(){
    	$itemListType=$this->getRequest()->getParam('itemlisttype');
        
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
        $db = Zend_Db::factory($config->resources->db);
        $selecter=$db->select()->from('txy_taobao_items',array('num_iid','title','item_url_spell','price','is_promotion','promotion_price','promotion_name','pic_url','freight_payer'));
        		//$adapter=new Txy_Extends_TempTaobaoPageinator($db->select()->from('txy_temp_taobao')->where('item_status=2'));
        		if(null!=$itemListType){
	                $intItemListType=(int)$itemListType;
	        		if(0==$intItemListType){//hot
	        			$selecter->where('is_taobaoke=1')->order('count_num desc');
	        		}
        			if(1==$intItemListType){//new
	        			$selecter->where('is_taobaoke=1')->order('update_time desc');
	        		}
        			if(2==$intItemListType){//promotion
	        			$selecter->where('is_promotion=1')->order('add_time desc');
	        		}
        		}else{
        			$selecter->order('add_time desc');
        		}
                $adapter = new Zend_Paginator_Adapter_DbSelect($selecter);
                $paginator = new Zend_Paginator($adapter);
                $paginator->setItemCountPerPage(48);
                $paginator->setCurrentPageNumber($this->_getParam('page'));
                $this->view->paginator = $paginator;

    }
    
    public function tagAction(){
        $tags=$this->getRequest()->getParam('tags');
        $tags=urldecode($tags);
        //echo "tags is".$tags."<br/>";
        @trigger_error('error', E_USER_NOTICE);
        $temp1=@iconv("GBK","UTF-8",$tags);
        //echo "temp1 is".$temp1."<br/>";
        $error=error_get_last();
        //var_dump($error);
        if($error['message']!='error'){
            $tags=urldecode($tags);
        }else{
            $tags=@iconv("GBK","UTF-8",$tags);
        }
        //echo $tags;
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
        $db = Zend_Db::factory($config->resources->db);
        
        $selecterLevel0=$db->select()->from('txy_item_characters_list',array('level0'))->group('level0');
        $res0=$db->fetchAll($selecterLevel0);
        $this->view->level0=Txy_Util_Character::uniqueCharacter($res0, 'level0');
        
        $selecterLevel1=$db->select()->from('txy_item_characters_list',array('level1'))->group('level1');
        $res1=$db->fetchAll($selecterLevel1);
        $this->view->level1=Txy_Util_Character::uniqueCharacter($res1, 'level1');
        
        $selecterLevel2=$db->select()->from('txy_item_characters_list',array('level2'))->group('level2');
        $res2=$db->fetchAll($selecterLevel2);
        $this->view->level2=Txy_Util_Character::uniqueCharacter($res2, 'level2');
        
        $selecterLevel3=$db->select()->from('txy_item_characters_list',array('level3'))->group('level3');
        $res3=$db->fetchAll($selecterLevel3);
        $this->view->level3=Txy_Util_Character::uniqueCharacter($res3, 'level3');
        
        $selecterLevel4=$db->select()->from('txy_item_characters_list',array('level4'))->group('level4');
        $res4=$db->fetchAll($selecterLevel4);
        $this->view->level4=Txy_Util_Character::uniqueCharacter($res4, 'level4');
        try{
            if(null!=$tags){
                if(stristr($tags, "-")){
        	        $arrayTags=explode("-", $tags);
        	        foreach ($arrayTags as $tag){
        	             $res=Txy_Util_Character::getCharacterLevelRes($tag);
        	             if(""!=$res){
        	                 $currentTags[$res['level']]=$res['name'];
        	             }
        	        }
    	            $this->view->currentTags=$currentTags;
                    //设置title
                    $title=str_replace("-", " - ", $tags);
    	            $this->view->headTitle('标签分类 - '.$title);
    	            //设置关键词（seo）
    	            $keywords=str_replace("-", ",", $tags);
    	            $this->view->headMeta()->appendName('keywords', $keywords);
    	            //设置描述（seo）
    	            $this->view->headMeta()->appendName('description','通过'.$keywords.'标签组合对商品进行分类筛选所得商品列表。');
                }else{
                    $res=Txy_Util_Character::getCharacterLevelRes($tags);
                    if(""!=$res){
                        $currentTags[$res['level']]=$res['name'];
                    }
                    $this->view->currentTags=$currentTags;
                    //设置title
    	            $this->view->headTitle('标签分类 - '.$tags);
    	            //设置关键词（seo）
    	            $this->view->headMeta()->appendName('keywords', $tags);
    	            //设置描述（seo）
    	            $this->view->headMeta()->appendName('description','通过'.$tags.'标签对商品进行分类筛选所得商品列表。');
                }
                $sqlWhere=Txy_Util_Character::getCharacterWhereSql($tags);
                $selecterNum=$db->select()->from('txy_items_join_character','count(*)')->where($sqlWhere);
                $resNum=$db->fetchOne($selecterNum);
                if((int)$resNum>0){
                    $selecter=$db->select()->from('txy_items_join_character')->where($sqlWhere);
                    $this->view->ResultsNum=$resNum;
                }else{
                    $selecter=$db->select()->from('txy_taobao_items')->order("count_num desc");
                    $this->view->ResultsNum=0;
                }
                
            }else{
                $selecter=$db->select()->from('txy_taobao_items')->order("count_num desc");
                //设置title
                $this->view->headTitle('标签分类');
                //设置关键词（seo）
                $this->view->headMeta()->appendName('keywords', "标签分类,商品标签,商品分类");
                //设置描述（seo）
                $this->view->headMeta()->appendName('description','通过商品标签对商品进行分类，用户通过标签组合可以筛选期望的目标商品。');
                
                
            }
        }catch(Exception $e){
            if("1"==$e->getCode()){
                $selecter=$db->select()->from('txy_taobao_items')->order("count_num desc");
                //设置title
                $this->view->headTitle('标签分类');
                //设置关键词（seo）
                $this->view->headMeta()->appendName('keywords', "标签分类,商品标签,商品分类");
                //设置描述（seo）
                $this->view->headMeta()->appendName('description','通过商品标签对商品进行分类，用户通过标签组合可以筛选期望的目标商品。');
                 //设置标签异常,返回默认结果
                $this->view->ResultsNum=-1;
            }else{
                throw $e;
            }
            
        }
            $adapter = new Zend_Paginator_Adapter_DbSelect($selecter);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(16);
            $paginator->setCurrentPageNumber($this->_getParam('page'));
            $this->view->paginator = $paginator;
        
        

        
    }
    
    public function priceChartAction(){
        $numiid=$this->getRequest()->getParam('id');
        $chart=new Txy_Util_PriceChart();
        $chart->drawChart($numiid);
        $this->view->numiid=$numiid;
        
        
        $db_taobaoitems=new Application_Model_DbTable_TaobaoItems();
        $item=$db_taobaoitems->getItemsByNumiid($numiid);
        $db_priceMark=new Application_Model_DbTable_Pricemark();
        $itemPriceMark=$db_priceMark->getPriceByNumiid($numiid);
        if(null!=$itemPriceMark){
            if($itemPriceMark['min_price']!=$itemPriceMark['max_price']){
                $item['priceMark']=$itemPriceMark;
            }
        }
        //设置title
        $this->view->headTitle()->prepend($item['title']);
        //设置关键词（seo）
        $this->view->headMeta()->appendName('keywords', $item['item_keywords']);
        //设置描述（seo）
        $this->view->headMeta()->appendName('description',$item['title'].'价格信息汇总。');
        $keywords=explode(",", $item['item_keywords']);
        $this->view->keywds=$keywords;
        $this->view->item=$item;
        
        
        
        
        
    }
    

    

    
    


}



